package tec.stan.ling.topo.graph.editpart;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;

/**
 * As the Controller, an EditPart ties the application's model to a visual
 * representation. EditParts are responsible for making changes to the model.
 * 
 * Deal with adapter of emf.
 * 
 * @author Stan 张新潮
 * @since 2009-12-31
 */
public abstract class AbstractNotifierEditPart extends
		AbstractGraphicalEditPart implements Adapter {

	private Notifier target;

	/**
	 * tip: Create figure for showing.
	 * 
	 * @Override protected IFigure createFigure() { return new Figure(); }
	 * 
	 *           tip: Create policy for command.
	 * @Override protected protected void createEditPolicies() {
	 * 
	 *           tip: add this policy for delete EditPart itself
	 *           installEditPolicy(EditPolicy.COMPONENT_ROLE, new
	 *           CanvasComponentEditPolicy());
	 * 
	 *           tip: if EditPart have children, add this policy for create
	 *           children installEditPolicy(EditPolicy.LAYOUT_ROLE, new
	 *           ComponentsXYLayoutEditPolicy()); }
	 * 
	 *           tip: return children
	 * @Override protected List<Object> getModelChildren() { tip: if EditPart
	 *           have children, you must return the children here List<Object>
	 *           result = new ArrayList<Object>();
	 *           result.addAll(getModel().getChildren()); return result; }
	 * 
	 *           tip: it will be called when the model modified.
	 * @Override public void notifyChanged(Notification notification) { tip: get
	 *           feature id in EMF int featureId =
	 *           notification.getFeatureID(QuizzePackage.class); switch
	 *           (featureId) { case QuizzePackage.CANVAS__WIDTH: ...... tip:
	 *           when model's attribute modify refreshVisuals(); break; case
	 *           QuizzePackage.CANVAS__TEXT: ...... tip: when model's children
	 *           modify refreshChildren(); break; } }
	 */

	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		super.refreshVisuals();
		// notify parent container of changed position & location
//		if (getModel() instanceof IConstrainable) {
//			Rectangle bounds = FigureUtil
//					.getRectangle((IConstrainable) getModel());
//			((GraphicalEditPart) getParent()).setLayoutConstraint(this,
//					getFigure(), bounds);
//		}
	}

	@Override
	public void activate() {
		super.activate();
		EObject object = (EObject) getModel();
		object.eAdapters().add(this);
	}

	@Override
	public void deactivate() {
		super.deactivate();
		EObject object = (EObject) getModel();
		object.eAdapters().remove(this);
	}

	@Override
	public Notifier getTarget() {
		return target;
	}

	@Override
	public boolean isAdapterForType(Object type) {
		return type.equals(getModel().getClass());
	}

	@Override
	public void setTarget(Notifier newTarget) {
		this.target = newTarget;
	}
}
